package Lotr.game;

import Lotr.game.Entity.Enemy;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.util.List;

public class Animation {

    private List[] sprites = new List[4];
    private Enemy enemy;
    private int currentImage;
    private int prevDirection;
    private int counter;

    public Animation(Enemy enemy) {
        this.enemy = enemy;
        whichEnemy();
        currentImage = 0;
        prevDirection = -1;
        counter = 1;
    }

    private void whichEnemy() {
        int tempEnemyType = enemy.getType();
        if (tempEnemyType == Enemy.TROLL) {
            sprites = enemy.getResources().getTrollSprites();
        }else if(tempEnemyType == Enemy.DRAGON){
            sprites = enemy.getResources().getDragonSprites();
        }else if(tempEnemyType == Enemy.ORK){
            sprites = enemy.getResources().getOrkSprites();
        }else if(tempEnemyType == Enemy.URUK){
            sprites = enemy.getResources().getUrukSprites();
        }else if(tempEnemyType == Enemy.WARG){
            sprites = enemy.getResources().getWargSprites();
        }
    }

    public void nextImage() {
        counter++;
        if (counter >= 3) {
            counter = 1;
            currentImage++;
        }
        if (prevDirection != enemy.getDirection()) {
            currentImage = 0;
            counter = 1;
        }
        if (sprites[enemy.getDirection()].size() <= currentImage) {
            currentImage = 0;
            counter = 1;
        }
        prevDirection = enemy.getDirection();
    }

    public Image getCurrentImage() {
        if(sprites[enemy.getDirection()].isEmpty()){
            Image i = new BufferedImage(enemy.getWidth(), enemy.getHeight(), BufferedImage.TYPE_INT_RGB);
            Graphics g = i.getGraphics();
            g.setColor(Color.MAGENTA);
            g.fillRect(0, 0, enemy.getWidth(), enemy.getHeight());;
            return i;
        }
        return (Image) sprites[enemy.getDirection()].get(currentImage);
    }
}
